package org.gamma.example.site.client;

import org.gamma.gwt.GammaWidget;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;

public class GammaExamplePanel extends VerticalPanel {
    private final TextArea text = new TextArea();
    private final Button button = new Button("Send Message");
    private final GammaWidget gaw = new GammaWidget(
            "org.gamma.example.applet.TestApplet", 
            "testApplet", 
            "gamma-example-applet.jar, " +
            "plugin.jar, " + 
            "gamma-1.0.jar",
            null, 
            850, 
            350);
    
    private int number = 0;
    
    public GammaExamplePanel() {
    	button.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
            	Request req = JavaScriptObject.createObject().cast();
            	req.setNumber(number++);
            	text.setText(text.getText() + "\nCreated request number " + req.getNumber() + ". Publishing to applet...");
                gaw.publish(req, new AsyncCallback<JavaScriptObject>() {
					@Override
					public void onFailure(Throwable caught) {
						GWT.log("Exception", caught);
						text.setText(text.getText() + "\nCaught an exception. See log for details.");
					}

					@Override
					public void onSuccess(JavaScriptObject result) {
						Response res = result.cast();
						text.setText(text.getText() + '\n' + res.getResponse());
					}
                });
			}
		});
        
        add(button);
        text.setVisibleLines(20);
        text.setWidth("840px");
        add(text);
        add(gaw);
    }
}
